package com.example.translateappjava.Activity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.translateappjava.DataBase.UserDbHelper;
import com.example.translateappjava.R;
import android.app.DatePickerDialog;
import android.widget.DatePicker;

import java.time.LocalDate;

public class RegisterActivity extends AppCompatActivity {


    private EditText et_username;
    private EditText et_password;

    private Spinner sp_sex;

    private TextView et_birth;
    private EditText et_name;
    private SharedPreferences mSharedPreferences;
    private EditText et_repassword;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);

        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_birth = findViewById(R.id.et_birth);
        sp_sex = findViewById(R.id.sp_sex);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.sex_options, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        sp_sex.setAdapter(adapter);
        et_name = findViewById(R.id.et_name);
        et_repassword = findViewById(R.id.et_repassword);
        mSharedPreferences = getSharedPreferences("user",MODE_PRIVATE);

        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                String repassword = et_repassword.getText().toString();
                String birth = et_birth.getText().toString();
                String sex = sp_sex.getSelectedItem().toString();
                String name = et_name.getText().toString();
                LocalDate currentDate = LocalDate.now();
                String sDate = currentDate.toString();


                // 检查用户名是否已存在
                if (UserDbHelper.getInstance(RegisterActivity.this).checkUsernameExists(username)) {
                    Toast.makeText(RegisterActivity.this, "用户名已存在，请选择其他用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 正则表达式验证


                if(!password.matches(".{6,}")) {
                    Toast.makeText(RegisterActivity.this, "密码至少需要6个字符", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(!birth.matches("\\d{4}-\\d{2}-\\d{2}")) {
                    Toast.makeText(RegisterActivity.this, "生日格式不正确，正确格式为: YYYY-MM-DD", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(!sex.matches("男|女")) {
                    Toast.makeText(RegisterActivity.this, "性别输入不正确，请输入'男'或'女'", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(!repassword.equals(password)){
                    Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(username) && TextUtils.isEmpty(password)){
                    Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{
//                    SharedPreferences.Editor edit = mSharedPreferences.edit();
//                    edit.putString("username",username);
//                    edit.putString("password",password);

                    int register = UserDbHelper.getInstance(RegisterActivity.this).register(username, password, "暂无~~~", sex, birth, name, sDate);
                    int row = register;

                    if(row > 0){
                        Toast.makeText(RegisterActivity.this, "注册成功，请登录", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                }
            }
        });
        et_birth = findViewById(R.id.et_birth);
        et_birth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog();
            }
        });



    }

    private void showDatePickerDialog() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue() - 1; // DatePickerDialog中月份是从0开始的
        int day = currentDate.getDayOfMonth();

        // 创建一个DatePickerDialog实例并显示
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // 用户选择的日期
                String formattedDate = String.format("%d-%02d-%02d", year, month + 1, dayOfMonth);
                et_birth.setText(formattedDate); // 将格式化后的日期显示在EditText中
                et_birth.setTextSize(18);
            }
        }, year, month, day);

        datePickerDialog.show();
    }


}